#pragma once
// 基于文件描述符的封装
// 用于通过fd进行 读 写 异常 的处理
// 每个Connection记录自己需要被关心的事假
#include "sys/epoll.h"

namespace wxd
{
    class Reactor;
    class Connection
    {
    public:
        Connection():_sockfd(-1)
        {
        }

        //读方法
        virtual void Recv() = 0;        
        //写方法
        virtual void Send() = 0;
        //异常处理方法
        virtual void Except() = 0;

        void SetEvents(uint32_t events)
        {
            _events = events;
        }
        uint32_t GetEvents()
        {
            return _events;
        }

        int Sockfd()
        {
            return _sockfd;
        }

        void SetSockfd(int sockfd)
        {
            _sockfd = sockfd;
        }

        void SetOwner(Reactor* reactor_p)
        {
            _owner = reactor_p;
        }

        void AppendInbuffer(char* msg)
        {
            _inbuffer += msg;
        }

        std::string& GetInbuffer()
        {
            return _inbuffer;
        }

        void AppendOutBuffer(std::string & resp_str)
        {
            _outbuffer += resp_str;
        }
        std::string& GetOutBuffer()
        {
            return _outbuffer;
        }

        void Close()
        {
            close(_sockfd);
        }

    protected:
        int _sockfd;
        uint32_t _events;
        Reactor* _owner;
        std::string _inbuffer;
        std::string _outbuffer;
    };
}